"""
信息面板组件
显示应用名称、版本号和版权信息
从pyproject.toml配置文件中动态读取项目信息
"""

import sys
from pathlib import Path

try:
    import tomllib  # Python 3.11+
except ImportError:
    import tomli as tomllib  # Python 3.10及以下

from PySide6.QtWidgets import QWidget, QVBoxLayout, QLabel
from PySide6.QtCore import Qt


class InfoPanel(QWidget):
    """信息面板组件"""
    
    def __init__(self):
        super().__init__()
        self.project_info = self._load_project_info()
        self._setup_ui()
    
    def _load_project_info(self):
        """
        从pyproject.toml加载项目信息
        
        Returns:
            dict: 包含项目名称、版本、描述、版权等信息的字典
        """
        # 获取项目根目录
        if getattr(sys, 'frozen', False):
            project_root = Path(sys.executable).parent
        else:
            project_root = Path(__file__).parent.parent.parent.parent
        
        pyproject_path = project_root / "pyproject.toml"
        
        with open(pyproject_path, 'rb') as f:
            config = tomllib.load(f)
            
        project = config['project']
        return {
            'name': project['name'],
            'version': project['version'],
            'description': project['description'],
            'copyright': project.get('copyright', '© 2024 色块下载器')
        }
    
    def _setup_ui(self):
        """设置UI界面"""
        layout = QVBoxLayout(self)
        layout.setAlignment(Qt.AlignCenter)
        
        # 应用名称
        app_name = self.project_info['description'] or self.project_info['name']
        app_name_label = QLabel(app_name)
        app_name_label.setAlignment(Qt.AlignCenter)
        app_name_label.setStyleSheet("font-weight: bold; font-size: 14px;")
        layout.addWidget(app_name_label)
        
        # 版本信息
        version_label = QLabel(f"版本: v{self.project_info['version']}")
        version_label.setAlignment(Qt.AlignCenter)
        version_label.setStyleSheet("color: #666666; font-size: 12px;")
        layout.addWidget(version_label)
        
        # 版权信息
        copyright_label = QLabel(self.project_info['copyright'])
        copyright_label.setAlignment(Qt.AlignCenter)
        copyright_label.setStyleSheet("color: #999999; font-size: 10px;")
        layout.addWidget(copyright_label)